<<<<<<< .mine
<?php

namespace CHOMP\SGIBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use CHOMP\SGIBundle\Entity\Empresa;
use CHOMP\SGIBundle\Form\EmpresaType;
use Pagerfanta\Pagerfanta;
use Pagerfanta\Adapter\DoctrineORMAdapter;

/**
 * Empresa controller.
 *
 * @Route("/empresa")
 */
class EmpresaController extends Controller
{

    /**
     * Lists all Empresa entities.
     *
     * @Route("/index/{page}", name="empresa_index", requirements={"page" = "\d+"}, defaults={"page" = "1"})
     * @Method("GET")
     * @Template("CHOMPSGIBundle:Empresa:index.html.twig")
     */
    public function indexAction($page)
    {
        $em = $this->getDoctrine()->getManager();
		$query = $em->createQueryBuilder()->select('u')->from('CHOMPSGIBundle:Empresa','u')->where('u.borrado = false');
		
		$pagerfanta = new Pagerfanta(new DoctrineORMAdapter($query));
		$pagerfanta->setMaxPerPage(10);
		$pagerfanta->setCurrentPage($page);
		
		return array('entities' => $pagerfanta);
    }
    /**
     * Creates a new Empresa entity.
     *
     * @Route("/", name="empresa_create")
     * @Method("POST")
     * @Template("CHOMPSGIBundle:Empresa:new.html.twig")
     */
    public function createAction(Request $request)
    {
        $entity = new Empresa();
		$entity->setBorrado(false);
        $form = $this->createCreateForm($entity);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('empresa_show', array('id' => $entity->getId())));
        }

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
    * Creates a form to create a Empresa entity.
    *
    * @param Empresa $entity The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createCreateForm(Empresa $entity)
    {
        $form = $this->createForm(new EmpresaType(), $entity, array(
            'action' => $this->generateUrl('empresa_create'),
            'method' => 'POST',
        ));

        return $form;
    }

    /**
     * Displays a form to create a new Empresa entity.
     *
     * @Route("/new", name="empresa_new")
     * @Method("GET")
     * @Template()
     */
    public function newAction()
    {
        $entity = new Empresa();
        $form   = $this->createCreateForm($entity);

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
     * Finds and displays a Empresa entity.
     *
     * @Route("/{id}", name="empresa_show")
     * @Method("GET")
     * @Template()
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('CHOMPSGIBundle:Empresa')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Empresa entity.');
        }

        return array(
            'entity'      => $entity,
        );
    }

    /**
     * Displays a form to edit an existing Empresa entity.
     *
     * @Route("/{id}/edit", name="empresa_edit")
     * @Method("GET")
     * @Template()
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('CHOMPSGIBundle:Empresa')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Empresa entity.');
        }

        $form = $this->createEditForm($entity);

        return array(
            'entity'      => $entity,
            'form'   => $form->createView(),
        );
    }

    /**
    * Creates a form to edit a Empresa entity.
    *
    * @param Empresa $entity The entity
    *
    * @return \Symfony\Component\Form\Form The form
    */
    private function createEditForm(Empresa $entity)
    {
        $form = $this->createForm(new EmpresaType(), $entity, array(
            'action' => $this->generateUrl('empresa_update', array('id' => $entity->getId())),
            'method' => 'PUT',
        ));

        return $form;
    }
    /**
     * Edits an existing Empresa entity.
     *
     * @Route("/{id}", name="empresa_update")
     * @Method("PUT")
     * @Template("CHOMPSGIBundle:Empresa:edit.html.twig")
     */
    public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('CHOMPSGIBundle:Empresa')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Empresa entity.');
        }

        $form = $this->createEditForm($entity);
        $form->handleRequest($request);

        if ($form->isValid()) {
            $em->flush();

            return $this->redirect($this->generateUrl('empresa_edit', array('id' => $id)));
        }

        return array(
            'entity'      => $entity,
            'form'   => $form->createView(),
        );
    }
    /**
     * Deletes a Empresa entity.
     *
     * @Route("/delete/{id}", name="empresa_delete")
     * @Method("GET")
     */
	public function deleteAction($id) {
		
		$em = $this->getDoctrine()->getManager();
		$idsArray = explode (',', $id);

		for ($i = 0; $i < count($idsArray); $i++){
			$empABorrar = $this->getDoctrine()->getRepository('CHOMPSGIBundle:Empresa')->find($idsArray[$i]);
			$empABorrar->setBorrado(TRUE);
			$em->persist($empABorrar);
		}

		$em->flush();
				
		return $this->redirect($this->generateUrl('empresa_index'));	
	}
	
}
=======
<?php

namespace CHOMP\SGIBundle\Controller;

use CHOMP\SGIBundle\Form\EmpresaType;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use CHOMP\SGIBundle\Entity\Empresa;
use CHOMP\SGIBundle\Entity\CuentaAnalitica;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\CallbackValidator;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormError;
use Pagerfanta\Pagerfanta;
use Pagerfanta\Adapter\DoctrineORMAdapter;


class EmpresaController extends Controller
{
		
	public function listarAction($page)
	{
		$em = $this->getDoctrine()->getManager();
		$query = $em->createQueryBuilder()->select('u')->from('CHOMPSGIBundle:Empresa','u')->where('u.borrado = false');
		
		$paginador = new Pagerfanta(new DoctrineORMAdapter($query));
		$paginador->setMaxPerPage(3);
		$paginador->setCurrentPage($page); 
		
		return $this->render('CHOMPSGIBundle:Empresa:listar.html.twig', array('empresas' => $paginador));
	}	
	
	public function verAction($id)
	{
		$empresa = $this->getDoctrine()->getRepository('CHOMPSGIBundle:Empresa')->find($id);
				
		return $this->render('CHOMPSGIBundle:Empresa:ver.html.twig', array('empresa' => $empresa));
	}
	
	
	/*
	public function nuevoAction()
	{
			$empresa = new Empresa();
				
			$form = $this->createFormBuilder($empresa,array('cascade_validation' => true))
				->add('nombre','text')
				->add('descripcion','text')
				->add('direccion','text')
				->add('telefono','text')
				->getForm();
	
			return $this->render('CHOMPSGIBundle:Empresa:nuevo.html.twig', array(
				'form' => $form->createView(),
			'empresa' => $empresa));
	}
	*/
	

	public function nuevoAction(Request $request)
	{
		$empresa = new Empresa();
		$empresa->setBorrado(false);
	
		$form = $this->createForm(new EmpresaType(), $empresa);
	
		$form->handleRequest($request);
		
		if ($form->get('save')->isClicked()) {
			if ($form->isValid()) {

				$em = $this->getDoctrine()->getManager(); 
				$em->persist($empresa);
				$em->flush();
			
				//return new Response('Empresa creada con el ID:'.$empresa->getId());
				return $this->redirect($this->generateUrl('empresa_listar'));	
			}
		}

		return $this->render('CHOMPSGIBundle:Empresa:nuevo.html.twig', array(
			'form' => $form->createView(),
			//'empresa' => $empresa));	
			));

	}


	public function successAction()
	{
		return $this->render('CHOMPSGIBundle:Empresa:success.html.twig');
	}

	public function borrarAction($id) {
		
		$em = $this->getDoctrine()->getManager();
		$idsArray = explode (',', $id);

		for ($i = 0; $i < count($idsArray); $i++){
			$empABorrar = $this->getDoctrine()->getRepository('CHOMPSGIBundle:Empresa')->find($idsArray[$i]);
			$empABorrar->setBorrado(TRUE);
			$em->persist($empABorrar);
		}

		$em->flush();
				
		return $this->redirect($this->generateUrl('empresa_listar'));	
	}



	public function editarAction($id)
	{
		$em = $this->getDoctrine()->getManager();
		$empresa = $em->getRepository('CHOMPSGIBundle:Empresa')->find($id);
				
		$form = $this->createFormBuilder($empresa)
			->add('id','hidden')
			->add('nombre','text')
			->add('descripcion','text')
			->add('telefono','text')
			->add('direccion','text')
			->getForm();

		return $this->render('CHOMPSGIBundle:Empresa:editar.html.twig', array(
			'form' => $form->createView(),
		'empresa' => $empresa));
	}

	public function editarPostAction(Request $request, $id)
	{
		$em = $this->getDoctrine()->getManager();
		$empresa = $em->getRepository('CHOMPSGIBundle:Empresa')->find($id);

		$form = $this->createFormBuilder($empresa)
			->add('nombre','text')
			->add('descripcion','text')
			->add('telefono','text')
			->add('direccion','text')
			->getForm();
	
		if ($request->isMethod('POST')) {
			$form->bind($request);
				
			$em->flush();

			return $this->redirect($this->generateUrl('empresa_listar'));
		}
	}

	public function successEditAction()
	{
		return $this->render('CHOMPSGIBundle:Empresa:successEdit.html.twig');
	}


	protected function getAllFormErrorMessages($form)
	{	
		$retval = array();
		foreach ($form->getErrors() as $key => $error) {
        		if($error->getMessagePluralization() !== null) {
            			$retval['message'] = $this->get('translator')->transChoice(
                		$error->getMessage(), 
                		$error->getMessagePluralization(), 
                		$error->getMessageParameters(), 
                		'validators'
            		);
        		} else {
            			$retval['message'] = $this->get('translator')->trans($error->getMessage(), array(), 'validators');
        		}
    		}
    		foreach ($form->all() as $name => $child) {
        		$errors = $this->getAllFormErrorMessages($child);
        			if (!empty($errors)) {
           				$retval[$name] = $errors; 
        			}
    		}
    	return $retval;
	}

}
>>>>>>> .r745
